// pages/huatidetail/huatidetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon:[
      {
        name:'点赞',
        icon:'good-job'
      },
      {
        name:'评论',
        icon:'chat',
        state:'pl',
      },
      {
        name:'收藏',
        icon:'star'
      }
    ],
    huati:{},
    pl:[],
    qita_huati:[],
    show:false,
    val:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  gethuati(id){
    let {icon,pl}=this.data
    wx.request({
      url: `http://127.0.0.1:7001/api/getzqhsqareid/${id}`,
      method:"GET",
      success:(res)=>{
        let data=res.data.data 
        console.log(data);
        icon[0].state=data.bang
        icon[2].state=data.soucang
        pl=data.pl.split(',')
        this.setData({
          icon,
          huati:data,
          pl
        })
      }
    })
  },
  // 更新详情 
  putdetail(detail){
    wx.request({
      url: `http://127.0.0.1:7001/api/putzqhsqareupdata/${detail.id}`,
      method:"PUT",
      data:detail,
      success:(res)=>{
        if(res.data.code==200){
          this.gethuati(detail.id)
          this.setData({
            val:'',
            show:false
          })
        }
      }
    })
  },
  onClose(){
    this.setData({
      show:!this.data.show
    })
  },
  onLoad(options) {
    let id=wx.getStorageSync('huati_id')
    let {qita_huati}=this.data
    this.gethuati(id)
    wx.request({
      url: 'http://127.0.0.1:7001/api/getzqhsqare',
      method:"GET",
      success:(res)=>{
        qita_huati=res.data.data.filter(v=>v.id!==id)
        this.setData({
          qita_huati
        })
      }
    })
  },
// 点赞 收藏 评论
onclicks(e){
  let {state,name}=e.currentTarget.dataset
  let {huati}=this.data
  if(isNaN(state)){
    this.onClose()
  }else{
    state=state=='0'?"1":'0'
    if(name=='点赞'){
      huati.bang=state
      huati.dianzan=state=='0'?huati.dianzan-1:(huati.dianzan-0)+1
    }else{
      huati.soucang=state
    }
    this.putdetail(huati)
  }
    
},
// 获取输入的值 
onchange(e){
  let {value}=e.detail
  this.setData({
     val:value.trim('')
  })
},
// 发布
fabu(){
  let {val,huati,pl}=this.data
  if(val){
    pl.unshift(val)
    huati.pl=pl.join(',')
    this.putdetail(huati)
  }else{
    wx.showToast({
      title:'请输入评论！',
      icon:'error'
    })
  }
},
// 我想要 已想要 
woyaoclick(){
  let {huati}=this.data
  huati.woyao=huati.woyao=='已想要'?'我想要':'已想要'
  this.putdetail(huati)
},
// 已关注 未关注 
guanzhu(){
  let {huati}=this.data
  huati.guanzhu=huati.guanzhu=='已关注'?'关注':'已关注'
  huati.fens=huati.guanzhu=='已关注'?(huati.fens-0)+1:huati.fens-1
  this.putdetail(huati)
},
// 跳转其他
toqita(e){
  let {id}=e.currentTarget.dataset
  wx.navigateTo({
    url: '/pages/huatidetail/huatidetail',
  })
  wx.setStorageSync('huati_id', id)
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})